﻿using Lazy.Captcha.Core.Generator.Code;
using Lazy.Captcha.Core.Generator.Image;
using Lazy.Captcha.Core.RateLimit;
using System;
using System.Collections.Generic;
using System.Text;

namespace Lazy.Captcha.Core
{
    /// <summary>
    /// 验证码帮助类
    /// </summary>
    public class CaptchaHelper
    {
        private static CaptchaService CaptchaService;

        public static void Initialization(CaptchaService captchaService)
        {
            CaptchaService = captchaService;
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="captchaId">验证码id</param>
        /// <param name="expirySeconds">缓存时间，未设定则使用配置时间</param>
        /// <returns></returns>
        public static CaptchaData Generate(string captchaId, int? expirySeconds = null)
        {
            return CaptchaService.Generate(captchaId, expirySeconds);
        }

        /// <summary>
        /// 校验
        /// </summary>
        /// <param name="captchaId">验证码id</param>
        /// <param name="code">用户输入的验证码</param>
        /// <param name="removeIfSuccess">校验成功时是否移除缓存(用于多次验证)</param>
        /// <param name="removeIfFail">校验失败时是否移除缓存</param>
        /// <returns></returns>
        public static bool Validate(string captchaId, string code, bool removeIfSuccess = true, bool removeIfFail = true)
        {
            return CaptchaService.Validate(captchaId, code, removeIfSuccess, removeIfFail);
        }

        /// <summary>
        /// 生成验证码（带频率限制）
        /// </summary>
        /// <param name="captchaId">验证码id</param>
        /// <param name="rateLimitKey">频率限制键（如IP地址、用户ID等）</param>
        /// <param name="expirySeconds">缓存时间，未设定则使用配置时间</param>
        /// <returns></returns>
        public static CaptchaData GenerateWithRateLimit(string captchaId, string rateLimitKey, int? expirySeconds = null)
        {
            return CaptchaService.GenerateWithRateLimit(captchaId, rateLimitKey, expirySeconds);
        }

        /// <summary>
        /// 检查频率限制
        /// </summary>
        /// <param name="rateLimitKey">频率限制键</param>
        /// <returns></returns>
        public static RateLimitResult CheckRateLimit(string rateLimitKey)
        {
            return CaptchaService.CheckRateLimit(rateLimitKey);
        }
    }
}
